<template>
  <div>
    <el-form @submit.native.prevent class="query-table-form" :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="借款人/担保客户查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          :auto-top="1"
          placeholder="地址查询"
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.bank_name"
          placeholder="银行名称查询"
          clearable
          @keyup.enter.native="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryDate"
          type="month"
          placeholder="选择月"
          :picker-options="pickerOptions"
          @change="handleDateQuery()"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumPostloanStatus"
          clearable
          @change="reSetParams"
          placeholder="项目状态"
          class="form-control-ml"
          v-model="load_beh_status"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('十二大产业贷')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" class="table-sm" :columns="tableColumns" :data="tableData">
      <template v-slot:use="scope">
        <div class="text-truncate" :title="scope.row.funds_use_dir">
          {{ scope.row.funds_use_dir }}
        </div>
      </template>
      <template v-slot:mainBudiness="scope">
        <div class="text-truncate" :title="scope.row.main_business_bank">
          {{ scope.row.main_business_bank }}
        </div>
      </template>
      <template v-slot:loanStatus="scope">
        <span class="status-label" :class="`badge-${scope.row.loanStatusStyle}`">
          {{ scope.row.loanStatusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      append-to-body
      top="30px"
      width="1100px"
      :close-on-click-modal="false"
      :visible.sync="oneIndustryVisible"
    >
      <industry-credit :one-industry="oneIndustry"></industry-credit>
    </el-dialog>
  </div>
</template>
<script>
import IndustryCredit from '@/bank/components/IndustryCredit'
import hndTableMixin from '@/bank/mixin/hndTable'
import SelectAddress from '@/components/select/SelectAddress'
import { hndBaseTableColumns, pickerOptions } from '@/bank/views/hnd/RecommendPass'
import { enumPostloanStatus, enumPostloanStatusLabel } from '@/bank/plugins/enum'

export default {
  components: {
    IndustryCredit,
    SelectAddress,
  },
  mixins: [hndTableMixin],
  props: {},
  data() {
    return {
      pickerOptions,
      enumPostloanStatus,
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        'bank_code.endlike': '',
        'load_beh_status.in': '1,5,6',
        'import_status.eq': 1,
      },
      order: {
        create_time: 'desc',
      },
      tableName: 'datum_one_county_industry',
      tableColumns: [
        ...hndBaseTableColumns,
        { attrs: { label: '贷后状态', align: 'center' }, slot: 'loanStatus' },
        {
          attrs: { label: '操作', align: 'center', width: '100px' },
          operations: [
            { content: '贷后详情', icon: 'fa fa-edit', event: this.showPostloanDetail },
            { content: '数字档案卡', icon: 'fa fa-search', event: this.showCreditFile },
          ],
        },
      ],
      load_beh_status: '',
      oneIndustry: {},
      oneIndustryVisible: false,
      queryDate: '',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.params['load_end_date.lte'] = this.$utils.dateFormat(new Date(), `yyyy-MM-dd`)
    this.getData()
  },
  methods: {
    handleDateQuery() {
      const startTime = this.queryDate
        ? `${this.queryDate.getFullYear()}-${this.queryDate.getMonth() + 1}-1`
        : ''
      const endTime = this.queryDate
        ? `${this.queryDate.getFullYear()}-${this.queryDate.getMonth() + 2}-1`
        : ''
      this.params['create_time.gte'] = startTime
      this.params['create_time.lt'] = endTime
      this.getData()
    },
    reSetParams(value) {
      if (!value) {
        this.params['load_beh_status.in'] = '5,6'
      } else {
        this.params['load_beh_status.in'] = value
      }
      this.getData()
    },
    dataAdapter(item) {
      item.loadDate = this.$utils.dateFormat(item.load_date, 'yyyy-MM-dd')
      item.loadEndDate = this.$utils.dateFormat(item.load_end_date, 'yyyy-MM-dd')

      const renewalStatus = enumPostloanStatusLabel[item.load_beh_status] || {}
      item.loanStatusLabel = renewalStatus.label
      item.loanStatusStyle = renewalStatus.css

      return item
    },
    showCreditFile(row = {}) {
      this.oneIndustry = row
      this.oneIndustryVisible = true
    },
    showPostloanDetail(row = {}) {
      this.$router.push({
        name: 'hndAfter',
        query: {
          id: row.customer_related_id,
          farmid: row.customer_id,
        },
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
